<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */

require_once ROOT_PATH.'/Abstract/front_abstract.php';

class Action extends front_abstract
{
    function index()
    {
         $param = array(
            'feature' => _GET('feature', 0),
            'count' => 10
         );

         if ($max_id = _GET('max_id')) {
            $param['max_id'] = sprintf('%1.0f', $max_id - 1);
         }

         if ($comments_to_me = _GET('comments_to_me')) {
            unset($param['feature']);
            $return = $this->api->comments_to_me($this->access_token,  $param);
            
            //字符转换表情
            $t = $this->api->emotions($this->access_token);
            $face = array();
            foreach ($t as $key => $val) {
                $face[$val['phrase']] = '<img src="'.$val['url'].'" />';
            }

            foreach ($return as &$val) {
                $val['text'] = $this->replace_content($val['text']);
            }
			if (empty($return)) {
               echo '';exit;
            }
            $this->smarty->assign('data', $return);

            $this->smarty->display('lib/comments.html');
         } elseif ($followers = _GET('followers')) {
             $param['user_id'] = _GET('user_id', 0);
             unset($param['feature']);
             
             if (isset($param['max_id'])) {
                $param['cursor'] = $param['max_id']+1;
                unset($param['max_id']);
             } else {
                $param['cursor'] = -1;
             }

             $return = $this->api->followers($this->access_token,  $param);
             // foreach ($return['users'] as &$value) { quq del
             //     $follow = $this->api->is_followed($this->oauth_token, $this->oauth_token_secret, $value['id']);
             //     $value['following'] = @$follow['source']['following'];
             // }
			if (empty($return)) {
               echo '';exit;
            }
             $this->smarty->assign('data', $return);
             $this->smarty->display('lib/fan_list.html');
         } elseif ($friends = _GET('friends')) {
           $param['user_id'] = _GET('user_id', 0);
           unset($param['feature']);
           
           if (isset($param['max_id'])) {
              $param['cursor'] = $param['max_id']+1;
              unset($param['max_id']);
           } else {
              $param['cursor'] = -1;
           }
           print_r($param);exit;
           $return = $this->api->friends($this->access_token,  $param);
           
           foreach ($return['users'] as &$value) {
                $follow = $this->api->is_followed($this->access_token, $value['id']);
                $value['following'] = @$follow['source']['following'];
            }
           $this->smarty->assign('data', $return);
           $this->smarty->display('lib/friends_list.html');
         } else {
             if ($user_id = _GET('user_id', 0)) {
                 $param['user_id'] = $user_id;
                 
                 $return = $this->api->user_timeline($this->access_token,  $param);
                 //print_r($return);exit;
                 
             } elseif ($mention = _GET('mention', 0)) {
                 unset($param['feature']);
                 $return = $this->api->mentions($this->access_token,  $param);
             } else {
                 if ($since_id = _GET('since_id')) {
                    $param['since_id'] = $since_id;                    
                 }
                 $return = $this->api->friends_timeline($this->access_token,  $param);
             }
             if (empty($return)) {
                echo '';exit;
             }
             $ids = array();

             foreach ($return as $key => $val) {
                $ids[] = $val['id'];

                if (isset($val['retweeted_status'])) {
                    $ids[] = $val['retweeted_status']['id'];
                }
             }

             // $data = $this->api->counts($this->oauth_token, $this->oauth_token_secret,  array_unique($ids));

             $t = $this->api->emotions($this->access_token);
             $face = array();
             foreach ($t as $key => $val) {
                $face[$val['phrase']] = '<img src="'.$val['url'].'" />';
             }

             foreach ($return as $key => $val) {
                $return[$key]['text'] = $this->replace_content($val['text']);
                // if (isset($data[$val['id']])) {
                //     $return[$key]['counts'] = $data[$val['id']];
                // } else {
                //     $return[$key]['counts'] = array(
                //         'comments' => 0,
                //         'rt' => 0
                //     );
                // }
                // if (isset($val['retweeted_status'])) {
                //     if (isset($data[$val['retweeted_status']['id']])) {
                //         $return[$key]['retweeted_status']['counts'] = $data[$val['retweeted_status']['id']];
                //     } else {
                //         $return[$key]['retweeted_status']['counts'] = array(
                //             'comments' => 0,
                //             'rt' => 0
                //         );
                //     }
                //     $return[$key]['retweeted_status']['text'] = $this->replace_content($val['retweeted_status']['text']);
                // }
             }
             $this->smarty->assign('data', $return);

             $this->smarty->display('lib/timeline.html');
         }
    }
}

?>